#pragma once

template<class K>
struct BSTreeNode
{
	BSTreeNode<K>* _left;
	BSTreeNode<K>* _right;
	K _key; 

	BSTreeNode(const K& key = K())
		:_left(nullptr)
		, _right(nullptr)
		, _key(key)
	{}
};

template<class K>
class BSTree
{
public:
	typedef BSTreeNode<K> Node;

	bool insert(const K& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			parent = cur;
			if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		if (parent->_key < key)
		{
			parent->_right = new Node(key);
		}
		else if (parent->_key > key)
		{
			parent->_left = new Node(key);
		}
		return true;
		
	}

	void _Inorder(Node* _root)
	{
		if (_root == nullptr)
		{
			return;
		}

		_Inorder(_root->_left);
		cout << _root->_key << " ";
		_Inorder(_root->_right);
	}

	void Inorder()
	{
		_Inorder(_root);
		cout << endl;
	}

	bool find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else
			{
				return true;
			}
		}
		return false;
	}

	bool erase(const K& key)
	{
		if (_root == nullptr)
			return false;

		Node* parent = _root;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				//查找到节点后
				if (cur->_left == nullptr)
				{
					if (cur == _root)
					{
						_root = cur->_left;
					}
					else
					{
						//左边为空
						if (parent->_left == cur)
							parent->_left = cur->_right;
						else
							parent->_right = cur->_right;
						return true;
					}
					delete cur;
				}
				else if (cur->_right == nullptr)
				{
					//右边为空
					if (cur == _root)
					{
						_root = cur->_left;
					}
					else
					{
						if (parent->_left == cur)
							parent->_left = cur->_left;
						else
							parent->_right = cur->_left;
						return true;
					}
					delete cur;
				}
				else
				{
					//都不为空
					//找到替换节点
					parent = cur;
					Node* subleft = cur->_right;
					while (subleft->_left != nullptr)
					{
						parent = subleft;
						subleft = subleft->_left;
					}

					swap(subleft->_key, cur->_key);

					if (parent->_left == subleft)
						parent->_left = subleft->_right;
					else
						parent->_right = subleft->_right;

					delete subleft;
				}
				return true;
			}
		}
		return false;
	}


	//递归版函数
	bool Find(const K& key)
	{
		return _Find(_root,key);
	}

	bool Insert(const K& key)
	{
		return _Insert(_root, key);
	}

	bool Erase(const K& key)
	{
		return _Erase(_root, key);
	}
private:
	bool _Find(Node* _root, const K& key)
	{
		if (nullptr == _root)
			return false;

		if (key > _root->_key)
		{
			return _Find(_root->_right, key);
		}
		else if (key < _root->_key)
		{
			return _Find(_root->_left, key);
		}
		else
		{
			return true;
		}
	}

	bool _Insert(Node*& _root, const K& key)
	{
		if (_root == nullptr)
		{
			_root = new Node(key);
			return true;
		}

		if (_root->_key < key)
		{
			return _Insert(_root->_right, key);
		}
		else if (_root->_key > key)
		{
			return _Insert(_root->_left, key);
		}
		else
		{
			return false;
		}
	}

	bool _Erase(Node*& _root, const K& key)
	{
		if (_root == nullptr)
			return false;

		if (_root->_key < key)
			return _Erase(_root->_right, key);
		else if(_root->_key > key)
			return _Erase(_root->_left, key);
		else
		{
			if (_root->_left == nullptr)
			{
				Node* del = _root;
				_root = _root->_right;
				delete del;
			}
			else if (_root->_right == nullptr)
			{
				Node* del = _root;
				_root = _root->_left;
				delete del;
			}
			else
			{
				Node* parent = _root;
				Node* subleft = _root->_right;
				while (subleft->_left != nullptr)
				{
					parent = subleft;
					subleft = subleft->_left;
				}

				swap(subleft->_key, _root->_key);

				if (parent->_left == subleft)
					parent->_left = subleft->_right;
				else
					parent->_right = subleft->_right;

				delete subleft;
			}
			return true;
		}
	}


private:
	Node* _root=nullptr;
};
